import numpy as np


def bboxpair2centerpair(trace_vals:np.array):
    bbox_body, bbox_head = trace_vals[:, :4], trace_vals[:, 4:]
    ctr_body = bbox_body[:, :2] + bbox_body[:, 2:] / 2
    ctr_head=  bbox_head[:, :2] + bbox_head[:, 2:] / 2
    return np.concatenate([ctr_body, ctr_head], axis=1)


def centerpair2speedpair(ctr_vals:np.array):
    prepad_ctr_vals = np.concatenate([np.zeros((1,4)), ctr_vals], axis=0)
    postpad_ctr_vals = np.concatenate([ctr_vals, np.zeros((1,4))], axis=0)
    l1_diff = (prepad_ctr_vals - postpad_ctr_vals)[1:-1]
    body_l2_diff = np.sqrt(np.sum(l1_diff[:, :2] ** 2, axis=1, keepdims=True))
    head_l2_diff = np.sqrt(np.sum(l1_diff[:, 2:] ** 2, axis=1, keepdims=True))
    l2_diff = np.concatenate([body_l2_diff, head_l2_diff], axis=1)
    return np.concatenate([np.zeros((1,2)), l2_diff][1:], axis=0)

def sumup(activities:list, sumup_interv =1800):
    n = activities.shape[0]
    res = []
    for i in range(0, n, sumup_interv):
        start_i, stop_i = i, i+sumup_interv
        res.append(np.sum(activities[start_i:stop_i], axis=0))
    return np.mat(res)

def average_daily(values, offset=0, npt_per_period=48):
    vals = values[offset:]
    n_terms = vals.shape[1]
    n_period = len(vals) // npt_per_period
    vals = vals[:n_period * npt_per_period]
    m = np.zeros((npt_per_period, n_terms), dtype=np.float)
    for i in range(n_period):
        m += vals[i*npt_per_period : (i+1)*npt_per_period]
    m /= n_period
    return m

